package arcane.object.gui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.image.BufferedImage;

public class DefaultButton extends DefaultLabel {
	BufferedImage background;
	Color bgColor;

	private int oldCAlpha;
	private int oldBGAlpha;

	public DefaultButton(String text) {
		this(text, Color.RED);
	}

	public DefaultButton(String text, Color color) {
		super(text, color);
		setInsets(new Insets(3, 3, 3, 3));
		bgColor = new Color(0f, 0f, 0f, 0f);
	}

	/**
	 * Overridden method of set enabled. Darkens text color and background color
	 * when component is disabled, brightens when enabled.
	 * 
	 * Repeatedly enabling or disabling component will not cause color to
	 * brighten or darken repeatedly.
	 */
	public void setEnabled(boolean enabled) {
		boolean old = this.enabled;
		this.enabled = enabled;
		if (old != enabled) {
			if (!enabled) {
				oldCAlpha = color.getAlpha();
				oldBGAlpha = bgColor.getAlpha();
				if (oldCAlpha != 0f)
					this.color = color.darker().darker().darker();
				if (oldBGAlpha != 0f)
					this.bgColor = bgColor.darker().darker().darker();
			} else {
				if (oldCAlpha != 0f)
					this.color = color.brighter().brighter().brighter();
				if (oldBGAlpha != 0f)
					this.bgColor = bgColor.brighter().brighter().brighter();
				color = new Color(color.getRed(), color.getGreen(), color
						.getBlue(), oldCAlpha);
				bgColor = new Color(bgColor.getRed(), bgColor.getGreen(),
						bgColor.getBlue(), oldBGAlpha);
			}
		}
	}

	public void setButtonBG(BufferedImage img) {
		background = img;
	}

	public BufferedImage getButtonBG() {
		return background;
	}

	public void setButtonBGColor(Color c) {
		bgColor = c;
	}

	public Color getButtonBGColor() {
		return bgColor;
	}

	public void renderBackground(Graphics2D g, int x, int y) {
		setOpenGLColor(bgColor,alpha);
		g.setColor(bgColor);
		if (background != null) {
			g.drawImage(background, x, y, null);
		} else {
			g.fillRect(x - inset.left, y - inset.top, getWidth() + inset.left,
					getHeight() + inset.top);
		}
	}

	public void render(Graphics2D g, int x, int y) {
		renderBackground(g, x, y);
		setOpenGLColor(color);
		super.render(g, x, y);
	}

	public void mouseEntered(int x, int y) {
		super.mouseEntered(x, y);
		this.setAlphaRange(.01f, .2f, .9f);
	}

	public void mouseExited(int x, int y) {
		super.mouseExited(x, y);
		setDeltaAlpha(0);
		setAlphaToggle(false);
		this.setAlphaMaximum(1);
		setAlphaMinimum(1);
		setAlpha(1);
	}
}